<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : message.manage.php   FILE_PATH : \\system\message.manage.php
 * ....后台短消息管理
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Sat Sep 22 15:54:07 CST 2007
 */

require_once("../global.inc.php");

class SysMessageManage extends SystemFrameWork{
	/**
	 * 短消息对象
	 *
	 * @var obj
	 */
	var $obj_message;

	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	/**
	 * 会员对象
	 *
	 * @var obj
	 */
	var $obj_member;

	function main(){
		/**
		 * 语言包
		 */
		$this->getlang("system_message");

		/**
		 * 创建短消息对象
		 */
		if (!is_object($this->obj_message)){
			require_once("message.class.php");
			$this->obj_message = new MessageClass();
		}

		/**
		 * 根据参数调用相应的方法
		 */
		switch ($this->_input['action']){
			case "list":
				$this->_list_message();
				break;
			case "add_message":
				$this->_add_message();
				break;
			case "save_message":
				$this->_save_message();
				break;
			case "view_message":
				$this->_view_message();
				break;
			case "del_message":
				$this->_del_message();
				break;
			default:
				$this->_list_message();
				break;
		}
	}

	/**
	 * 列表页面
	 */
	function _list_message(){
		/**
		 * 创建分页对象
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}

		$this->obj_page->pagebarnum(13);    //每页13条记录
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		//得到短消息列表
		$condition['system_state'] = 0;
		$condition['order'] = 1;
		$message_array = $this->obj_message->getMessage($condition,$this->obj_page,'*','message_system');


		if (is_array($message_array)){
			foreach ($message_array as $k => $v){
				$message_array[$k]['send_time'] = @date("Y-m-d",$v['send_time']);
			}
		}
		$pagelist = $this->obj_page->show(2);      //分页显示

		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('message_array',$message_array);
		$this->output('pagelist',$pagelist);
		$this->output('system_message_object',$this->_b_config['system_message_object']);/*群发对象类型*/
		$this->showpage('sys_message.list');
	}

	/**
	 * 新发短信息
	 */
	function _add_message(){
		/**
		 * 页面输出
		 */
		$this->output('system_message_object',Common::showForm_Radio('system_message_object','',$this->_b_config['system_message_object'],'0','radio'," onclick='send_message(this.value);' "));
		$this->output('system_message_delete',Common::showForm_Radio('system_message_delete','',$this->_b_config['system_message_delete'],'1','radio'," onclick='send_delete(this.value);' "));
		$this->showpage('sys_message.add');
	}

	/**
	 * 新发短信息
	 */
	function _save_message(){
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 创建会员对象
		 */
		if (!is_object($this->obj_member)){
			require_once ("member.class.php");
			$this->obj_member = new MemberClass();
		}

		$this->obj_validate->setValidate(array("input"=>$this->_input['title'],"require"=>"true","message"=>$this->_lang['errMessageTitle']));/*标题*/
//		$this->obj_validate->setValidate(array("input"=>$this->_input['content'],"require"=>"true","message"=>$this->_lang['errMessageContent']));/*内容*/
		if ($this->_input['system_message_object'] == '2'){
			$this->obj_validate->setValidate(array("input"=>$this->_input['system_receive_name'],"require"=>"true","message"=>$this->_lang['errMessageReceiveName']));/*发送会员名*/
		}
		if ($this->_input['system_message_delete'] == '0'){
			$this->obj_validate->setValidate(array("input"=>$this->_input['end_date'],"require"=>"true","message"=>$this->_lang['errMessageEndDate']));/*有效时间截止日期*/
		}
		$error = $this->obj_validate->validate();
		if ($error != "" ){
			$this->redirectPath('error','',$error,1);
		}
		
		//循环生成的批次
		$loop_num = $this->_input['loop_num']?$this->_input['loop_num']:0;

		//会员名称限制为10个
		$member_name_limit = 10;

		/*分割会员名*/
		$this->_input['system_receive_name'] = str_replace('，',',',$this->_input['system_receive_name']);
		$name_array = @explode(',',trim($this->_input['system_receive_name']));

		//去掉数组元素值为空的元素
		foreach ($name_array as $k1 => $v1){
			if ($v1 == '') {
				unset($name_array[$k1]);
			}
		}
		
		//截取10个会员名
		foreach ($name_array as $k2 => $v2){
			if ($k2 < $member_name_limit) {
				$name_str .= $v2 . ',';
			}
		}
		$name_str = substr($name_str,0,strlen($name_str)-1);
		
		if ($loop_num == 0) {
			/*发送短信息*/
			$input_param = array();
			$input_param['title'] = trim($this->_input['title']);/*标题*/
			$input_param['content'] = Common::replacebr($this->_input['content']);/*内容*/
			$input_param['send_object'] = $this->_input['system_message_object'];/*发送对象类型*/
			$input_param['system_receive_name'] = $name_str;/*发送会员名*/
			$message_system_id = $this->obj_message->addOneSystemMessage($input_param);
		}

		//每阶段生成100个
		$limit_start_num = $loop_num*100;
		$limit_num = $loop_num*100+100;
		//数量
		$condition['limit_start_num'] = $limit_start_num;
		$condition['limit_num'] = $limit_num;
		if ($this->_input['system_message_object'] == 0){/*所有用户*/
			
			/*取会员总数*/
			$member_count = $this->obj_member->getMemberList('',$page,"login_name","simple");
			/*取所有会员的用户名*/
			$member_array = $this->obj_member->getMemberList($condition,$page,"login_name","simple");
			
		}elseif ($this->_input['system_message_object'] == 1){/*所有卖家*/
			
			/*取卖家总数*/
			$member_count = $this->obj_member->getMemberList(array('member_type'=>1),$page,"login_name","simple");
			$condition['member_type'] = 1;
			$member_array = $this->obj_member->getMemberList($condition,$page,"login_name","simple");
			
		}elseif ($this->_input['system_message_object'] == 2){

			/*判断用户是否存在*/
			if (is_array($name_array)){
				foreach ($name_array as $k => $v){
					if ($k < $member_name_limit) {        //会员名称限制为10个
						$condition = '';
						$condition['member_name'] = $v;
						$flag = $this->obj_member->checkMemberExist($condition,'1');
						if ($flag !== true) {
							Common::outMessage("json",$v.$this->_lang['langMessageMemberNameErr'],2);
						}
						$member_array[]['login_name'] = $v;
					}
				}
			}
		}
		/*群发短信息*/
		/*有效时间截止日期*/
		if ($this->_input['system_message_delete'] == '0'){
			$time = @explode('-',$this->_input['end_date']);
			$time = @mktime(0,0,0,$time[1],$time[2],$time[0]);
		}
		if (!empty($member_array) && $this->_input['system_message_object'] != 2){
			foreach ($member_array as $v){
				$input_param = array();
				$input_param["member_name"] = 0;/*0为系统发信*/
				$input_param["txtReceive_name"] = $v['login_name'];/*接收人用户名*/
				$input_param["txtTitle"] = trim($this->_input['title']);/*短信息标题*/
				$input_param["txtContent"] = $message_system_id;/*关联群发表中内容ID*/
				$input_param["txtEndDate"] = $time;/*有效时间截止日期*/
				$input_param["rdoIsallowdel"] = $this->_input['system_message_delete'];/*是否允许删除*/
				$this->obj_message->addMessage($input_param);
			}
			Common::outMessage("json",$limit_num.'/'.count($member_count),1);
		}elseif (!empty($member_array) && $this->_input['system_message_object'] == 2) {
			foreach ($member_array as $key => $val){
				$input_param = array();
				$input_param["member_name"] = 0;/*0为系统发信*/
				$input_param["txtReceive_name"] = $val['login_name'];/*接收人用户名*/
				$input_param["txtTitle"] = trim($this->_input['title']);/*短信息标题*/
				$input_param["txtContent"] = $message_system_id;/*关联群发表中内容ID*/
				$input_param["txtEndDate"] = $time;/*有效时间截止日期*/
				$input_param["rdoIsallowdel"] = $this->_input['system_message_delete'];/*是否允许删除*/
				$this->obj_message->addMessage($input_param);
			}
		}
		/*记录操作日志*/
		SystemPowerClass::addSysLog($this->_lang['langMessageSysSetManageSend']);
		Common::outMessage("json",$this->_lang['langMessageSendInfoOk'],0);
	}

	/**
	 * 查看短信息内容
	 */
	function _view_message(){
		/*取短信息内容*/
		$condition['message_system_id'] = $this->_input['message_system_id'];
		$message_array = $this->obj_message->getOneMessage($condition,'*','message_system');
		$message_array['send_time'] = @date("Y-m-d",$message_array['send_time']);
		$curpage = $this->_input['curpage'];

		/**
		 * 页面输出
		 */
		$this->output("curpage", $curpage);
		$this->output('message_array',$message_array);
		$this->output('system_message_object',$this->_b_config['system_message_object']);/*群发对象类型*/
		$this->showpage('sys_message.view');
	}

	/**
	 * 删除短信息内容
	 */
	function _del_message(){
		if (is_array($this->_input['message_system_id'])){
			if ($this->obj_message->delSystemMessage($this->_input['message_system_id'])) {
				/*记录操作日志*/
				SystemPowerClass::addSysLog($this->_lang['langMessageSysSetManageDel']);
				$this->redirectPath('error','',$this->_lang['langMessageDelInfoOk'],1);
			}else {
				$this->redirectPath('error','',$this->_lang['langCOperatorLost'],1);
			}
		}else {
			$this->redirectPath('error','',$this->_lang['langMessagePleaseDelInfo'],1);
		}
	}
}

$message_manage = new SysMessageManage();
$message_manage->main();
unset($message_manage);
?>